package posti.test.domain;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;

public 	class BookBuilder {
	private List<Author> authors = new ArrayList<Author>();
	private String criticLastName;
	private String criticMiddleName;
	private String criticFirstName;
	private String reviewText;
	private int reviewPoints;
	private String isbn;
	private String title;
	private int id;
	
	public Book build() {
		try {
			Review review = new Review(reviewPoints);
			review.critic(populatePerson(criticFirstName, criticMiddleName, new Person(criticLastName)));
			review.reviewMethod(reviewText);
			Book book = new Book(title);
			book.isbn(isbn);
			Field field = Book.class.getDeclaredField("id");
			field.setAccessible(true);
			field.set(book, id);
			for (Author author : authors) {
				book.addAuthor(author);
			}
			book.setReview(review);
			return book;
		} 
		catch (Exception e) {
			throw new RuntimeException(e);
		}
	}

	private Author createAuthor(String name, String middleName, String lastName) {
		return (Author) populatePerson(name, middleName, new Author(lastName));
	}

	private Person populatePerson(String name, String middleName, Person person) {
		person.middleName(middleName);
		person.setFirstName(name);
		return person;
	}

	public void addAuthor(String first, String middle, String last) {
		authors.add(createAuthor(first, middle, last));
	}

	public void setCriticLastName(String criticLastName) {
		this.criticLastName = criticLastName;
	}

	public void setCriticMiddleName(String criticMiddleName) {
		this.criticMiddleName = criticMiddleName;
	}

	public void setCriticFirstName(String criticFirstName) {
		this.criticFirstName = criticFirstName;
	}

	public void setReviewText(String reviewText) {
		this.reviewText = reviewText;
	}

	public void setReviewPoints(int reviewPoints) {
		this.reviewPoints = reviewPoints;
	}

	public void setISBN(String isbn) {
		this.isbn = isbn;
	}

	public void setTitle(String title) {
		this.title = title;
	}

	public void setId(int id) {
		this.id = id;
	}
}
